//
//  M1ShuntViewController.m
//  NewMarryU
//
//  Created by apple on 2018/7/12.
//  Copyright © 2018年 Cedar. All rights reserved.
//

#import "M1ShuntVC.h"
#import "M1LoginVC.h"
#import "M1GenderViewController.h"
@interface M1ShuntVC ()<UIScrollViewDelegate>
@property (nonatomic ,strong) UIPageControl *m1_pageControl;
@end

@implementation M1ShuntVC
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [M1User remove];
    [[EMClient sharedClient] logout:YES];
}

-(void)m1_exp{
    M1GenderViewController *m1_genderVC = [M1GenderViewController new];
    [self.navigationController pushViewController:m1_genderVC animated:YES];
}

-(void)m1_login{
    M1LoginVC *m1_loginVC = [M1LoginVC new];
    m1_loginVC.m1_changeNavTitleColor = YES;
    m1_loginVC.m1_changeBackImage = YES;
    [self.navigationController pushViewController:m1_loginVC animated:YES];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    UIScrollView *m1_bgScroll = [[UIScrollView alloc]init];
    if (@available(iOS 11.0, *)) {
        m1_bgScroll.contentInsetAdjustmentBehavior = UIScrollViewContentInsetAdjustmentNever;
    }
    m1_bgScroll.showsVerticalScrollIndicator = 0;
    m1_bgScroll.showsHorizontalScrollIndicator = 0;
    m1_bgScroll.bounces = NO;
    m1_bgScroll.pagingEnabled = YES;
    [self.view addSubview:m1_bgScroll];
    [m1_bgScroll mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_offset(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    NSArray<M1ShuntImage *> *array = [M1SaveTool m1_appConfig].shunt_page;
    
    for (int i =0; i<array.count; i++) {
        M1ShuntImage *m1_shunImage = array[i];
        UIImageView *m1_imgView = [[UIImageView alloc] initWithFrame:CGRectMake(SCREENWIDTH*i,0,SCREENWIDTH,SCREENHEIGHT)];
        m1_imgView.contentMode = UIViewContentModeScaleAspectFill;
        [m1_imgView sd_setImageWithURL:[NSURL URLWithString:m1_shunImage.pic_image] placeholderImage:nil];
        m1_imgView.clipsToBounds = YES;
        m1_imgView.userInteractionEnabled = YES;
        [m1_bgScroll addSubview:m1_imgView];
    }
    m1_bgScroll.contentSize = CGSizeMake(SCREENWIDTH*array.count, 0);
    
    
    UIImage *m1_bgImg = [UIImage imageNamed:@"btn_color"];
    UIButton *m1_experienceBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [m1_experienceBtn setTitle:@"立即体验" forState:0];
    [m1_experienceBtn setBackgroundImage:m1_bgImg  forState:0];
    m1_experienceBtn.imageView.contentMode = UIViewContentModeScaleAspectFill;
    [m1_experienceBtn setTitleColor:[UIColor whiteColor] forState:0];
    m1_experienceBtn.titleLabel.font = m1_font(16);
    m1_experienceBtn.layer.cornerRadius = 25;
    m1_experienceBtn.clipsToBounds = YES;
    [m1_experienceBtn addTarget:self action:@selector(m1_exp) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:m1_experienceBtn];
    [m1_experienceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.bottom.offset(-117);
        make.size.mas_offset(CGSizeMake(270, 50));
    }];
    
    UIButton *m1_loginBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [m1_loginBtn setTitle:@"直接登录 >" forState:0];
    [m1_loginBtn setTitleColor:[UIColor m1_colorHex:0xFF5A3F] forState:0];
    m1_loginBtn.titleLabel.font = m1_font(15);
    [m1_loginBtn addTarget:self action:@selector(m1_login) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:m1_loginBtn];
    [m1_loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view);
        make.top.equalTo(m1_experienceBtn.mas_bottom).offset(15);
        make.size.mas_offset(CGSizeMake(270, 50));
    }];
    
    if (array.count >1) {
        m1_bgScroll.delegate = self;
        _m1_pageControl = [[UIPageControl alloc]init];
        _m1_pageControl.numberOfPages = array.count;
        _m1_pageControl.currentPage = 0;
        _m1_pageControl.pageIndicatorTintColor = m1_bg_color;
        _m1_pageControl.currentPageIndicatorTintColor = [UIColor m1_colorHex:0xFF597C];
        [self.view addSubview:_m1_pageControl];
        [_m1_pageControl mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(self.view);
            make.bottom.equalTo(m1_experienceBtn.mas_top).offset(-10);
        }];
    }
}
-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    _m1_pageControl.currentPage = (int)(scrollView.contentOffset.x/SCREENWIDTH);
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
